package com.legleg.servlet;

import com.legleg.data.*;
import com.legleg.data.Album;
import com.legleg.data.json.AlbumBean;
import com.legleg.handler.AlbumHandler;
import com.legleg.util.BaseUtil;
import com.legleg.util.RequestUtil;
import net.sf.json.JSONArray;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-26
 * Time: 下午5:05
 * To change this template use File | Settings | File Templates.
 */
public class Tag extends BaseServlet {
    private static List<String> sortedTags;

    @Override
    protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        String tag = RequestUtil.getStr(request, "t", null);
        if (tag != null && tag.length() > 0) {
            int page = RequestUtil.getInt(request, "p", 1);
            int size = RequestUtil.getInt(request, "s", 40);
            List<Album> albums = AlbumHandler.getInstance().queryByTag(tag, page, size);
            if (albums != null && albums.size() > 0) {
                ArrayList<AlbumBean> list = new ArrayList<AlbumBean>();
                for (Album album : albums) {
                    list.add(new AlbumBean(album.getId(), album.getTitle(), BaseUtil.getAlbumThumb(album.getId()), album.getTags(), album.getPicnum()));
                }
                JSONArray jarr = JSONArray.fromObject(list);
                if (jarr.size() > 0)
                    ctx.put("json", jarr.toString());
            }
        } else {
            getSortedTags();
            if (sortedTags != null && sortedTags.size() > 0) {
                int page = RequestUtil.getInt(request, "p", 1);
                int size = RequestUtil.getInt(request, "s", 40);
                int startIndex = (page - 1) * size;
                int endIndex = page * size;
                if (startIndex < (sortedTags.size() - 1)) {
                    if (endIndex > sortedTags.size()) endIndex = sortedTags.size();
                    List<String> tags = sortedTags.subList(startIndex, endIndex);
                    JSONArray json = JSONArray.fromObject(tags);
                    ctx.put("json", json.toString());
                }
            }
        }
        return getTemplate("tag.vm");
    }

    private List<String> getSortedTags() {
        if (sortedTags == null) {
            HashMap map = new HashMap();
            List<String> alltags = AlbumHandler.getInstance().queryAllTags();
            for (String trow : alltags) {
                if (StringUtils.trimToNull(trow) == null) continue;
                String[] ts = trow.split(",");
                for (String t : ts) {
                    Integer number = (Integer) map.get(t);
                    if (number == null) number = 0;
                    number++;
                    map.put(t, number);
                }
            }
            sortedTags = BaseUtil.getSortedList(map);
        }
        return sortedTags;
    }
}
